package com.cosmic.pizza.web.servlet;

import com.cosmic.pizza.exception.loginException;
import com.cosmic.pizza.pojo.ResultInfo;
import com.cosmic.pizza.pojo.User;
import com.cosmic.pizza.service.Impl.UserServiceImpl;
import com.cosmic.pizza.service.UserService;
import com.cosmic.pizza.util.Md5Util;
import com.cosmic.pizza.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/User/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserServiceImpl();

    public void checkUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean isExists = userService.usernameIsExists(username);
        ResultInfo info = new ResultInfo(!isExists);
        writeValue(resp, info);
    }

    // 用户登录 目前缺加密
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String check = req.getParameter("check");
        ResultInfo info = null;
        if (check != null && check.equalsIgnoreCase(req.getSession().getAttribute("CHECKCODE_SERVER").toString())) {
            try {
                password = Md5Util.encodeByMd5(password);
                User user = userService.login(username, password);
                // 登录成功, 将user存储到session中
                req.getSession().setAttribute("user", user);
                info = new ResultInfo(true);
            } catch (loginException e) {
                info = new ResultInfo(false, e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            info = new ResultInfo(false, "验证码错误");
        }
        writeValue(resp, info);
    }

    // 用户注册
    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        ResultInfo info = new ResultInfo();
        try {
            BeanUtils.populate(user, req.getParameterMap());
            user.setPassword(Md5Util.encodeByMd5(user.getPassword()));
            // 注册, 没有出现异常, 有可能成功, 也有可能失败
            boolean flag = userService.register(user);
            info.setFlag(flag);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            info.setFlag(false);
        }
        if (!info.isFlag()) {
            info.setErrorMsg("注册失败");
        }
        writeValue(resp, info);
    }

    public void active(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String code = req.getParameter("code");
        boolean flag = userService.activeUser(code);
        if (flag) {
            resp.sendRedirect(req.getContextPath() + "/active_ok.html");
        }else {
            resp.sendRedirect(req.getContextPath() + "/active_error.html");
        }
    }

    // 将用户存入session
    public void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object user = req.getSession().getAttribute("user");
        ResultInfo resultInfo = new ResultInfo();
        if (user == null) {
            resultInfo.setFlag(false);
        } else {
            resultInfo.setFlag(true);
            resultInfo.setData(user);
        }
        writeValue(resp, resultInfo);
    }
    public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().removeAttribute("user");
        ResultInfo info = new ResultInfo(true);
        writeValue(resp, info);
    }
}
